<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>权限列表</title>
    <link rel="stylesheet" type="text/css" href="/manager/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/manager/easyui/themes/icon.css">
    <script type="text/javascript" src="/manager/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/manager/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        //工具栏
        var toolbar = [{
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                $('#dd-add').dialog('open');

                //所有节点取消checked
                let nodes = $("#ah-add").tree('getChecked');
                for (const node of nodes) {
                    $('#ah-add').tree('uncheck', node.target);
                }
            }
        }, '-', {
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                $('#dd').dialog('open');
                let row = $("#userlist").datagrid("getSelected");
                $("#update-role-id").textbox("setValue", row.id);
                $("#update-role-name").textbox("setValue", row.name);

                //所有节点取消checked
                let nodes = $("#ah").tree('getChecked');
                for (const node of nodes) {
                    $('#ah').tree('uncheck', node.target);
                }
                // 添加该角色的 checked
                for (const permission of row.permissions) {
                    let node = $('#ah').tree('find', permission.id);
                    $('#ah').tree('check', node.target);
                }

            }
        }, '-', {
            text: '删除',
            iconCls: 'icon-cancel',
            handler: function () {
                let row = $("#userlist").datagrid("getSelected");
                $.ajax({
                    url: '/manager/role/delete',
                    type: "post",
                    data: {
                        id: row.id,
                    },
                    success: function (data) {
                        $('#userlist').datagrid("reload");
                    }
                });
            }
        }];
        $(function () {
            $('#userlist').datagrid({
                title: '角色列表',
                iconCls: 'icon-save',
                url: '/manager/role/findAll',
                singleSelect: true,
                columns: [
                    [
                        {field: 'id', title: '角色ID', width: 100, align: 'center'},
                        {field: 'name', title: '角色名称', width: 100, align: 'center'},
                        {
                            field: 'permissions',
                            title: '角色拥有的权限[权限名 | 权限URL | 模块名 | 操作名 ]',
                            width: 500,
                            align: 'center',
                            formatter: format
                        }
                    ]],
                pagination: true,
                toolbar: toolbar
            });
            //初始化查询栏
            $("#submit_search").linkbutton({iconCls: 'icon-search', plain: true})
                .click(function () {
                    console.log("search")
                    let data = {};
                    let name = $("#search-role-name").val();
                    if (name != "") {
                        data.name = name;
                    }
                    $('#userlist').datagrid("reload", data);
                });

            //初始化新增
            $('#dd-add').dialog({
                title: '新增角色',
                width: 700,
                height: 500,
                closed: true,
                cache: false,
                href: '',
                modal: true,
                iconCls: 'icon-save',
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: function () {
                        let nodes = $("#ah-add").tree('getChecked');
                        let ids = new Array();
                        for (let i = 0; i < nodes.length; i++) {
                            ids[i] = nodes[i].id;
                        }
                        $.post("/manager/role/add", {
                            name: $("#add-role-name").textbox("getValue"),
                            pids: ids
                        }, rs => {
                            if (rs.status != 200) {
                                alert(rs.msg);
                            } else {
                                $('#userlist').datagrid("reload");
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        $('#dd-add').dialog("close");
                    }
                }]
            });

            //初始化修改窗口
            $('#dd').dialog({
                title: '修改角色',
                width: 700,
                height: 500,
                closed: true,
                cache: false,
                href: '',
                modal: true,
                iconCls: 'icon-save',
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: function () {
                        let nodes = $("#ah").tree('getChecked');
                        let ids = new Array();
                        for (let i = 0; i < nodes.length; i++) {
                            ids[i] = nodes[i].id;
                        }
                        $.post("/manager/role/update", {
                            id: $("#update-role-id").textbox("getValue"),
                            name: $("#update-role-name").textbox("getValue"),
                            pids: ids
                        }, rs => {
                            if (rs.status != 200) {
                                alert(rs.msg);
                            } else {
                                $('#userlist').datagrid("reload");
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        $('#dd').dialog("close");
                    }
                }]
            });

            //tree选项框初始化
            $(".ah-tree").tree({
                url: "/manager/permission/findAllModulesWithPermission",
                method: 'post',
                animate: true,
                checkbox: true
            });
        });

        //列格式化方法
        function format(val, row) {
            let sele = "<select>";
            for (const perm of val) { //
                sele += "<option>角色拥有的权限[" + perm.name + " | " + perm.url + " | " + perm.module.name + " | " + perm.sname + "]</option>";
            }
            sele += "</select>";
            return sele;
        }
    </script>
</head>
<body>
<form name="searchform" method="post" action="" id="searchform">
    <td width="70" height="30"><strong>角色查询：</strong></td>
    <td height="30">
        <input type="text" name="keyword" id="search-role-name" size=20 placeholder="角色名称"/>
        <a id="submit_search">搜索</a>
    </td>
</form>
<table id="userlist"></table>

<div id="dd-add">
    <div align="center" style="margin-top: 30px">
        <form id="ff-add" method="post">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="name" id="add-role-name" style="width:50%"
                       data-options="label:'角色名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <table style="width:48%">
                    <td valign="top" style="width:50px">
                        权限:
                    </td>
                    <td>
                        <ul id="ah-add" class="ah-tree" name="pids"></ul>
                    </td>
                </table>
            </div>
        </form>
    </div>
</div>

<div id="dd">
    <div align="center" style="margin-top: 30px">
        <form id="ff" method="post">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="name" id="update-role-id" style="width:50%"
                       data-options="label:'角色ID:'"
                       readonly="readonly"/>
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="name" id="update-role-name" style="width:50%"
                       data-options="label:'角色名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <table style="width:48%">
                    <td valign="top" style="width:50px">
                        权限:
                    </td>
                    <td>
                        <ul id="ah" class="ah-tree"></ul>
                    </td>
                </table>
            </div>
        </form>
    </div>
</div>
</body>
</html>